<template>
  <div class="b-scroll" ref="wrapper">
    <slot></slot>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
export default {
  components: {},
  props: {
    probeType: {
      type: Number,
      default: 1
    },
    click: {
      type: Boolean,
      default: true
    },
    data: {
      type: Array,
      default: null
    },
    listenScroll: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
    }
  },
  watch: {
    data() {
      setTimeout(() => {
        this.refresh()
      }, 20)
    }
  },
  computed: {},
  methods: {
    _initScroll() {
      if (!this.$refs.wrapper) {
        return
      }
      const wrapper = this.$refs.wrapper
      this.scroll = new BScroll(wrapper, {
        click: this.click,
        probeType: this.probeType
      })
      // this.scroll.on('scrollStart', () => {
      //   debugger
      // })
      if (this.listenScroll) {
        let self = this
        this.scroll.on('scroll', (pos) => {
          self.$emit('scroll', pos)
        })
      }
      // if (this.listenScroll) {
      //   let me = this
      //   debugger
      //   this.scroll.on('scroll', (pos) => {
      //     me.$emit('scroll', pos)
      //   })
      // }
    },
    enable() {
      this.scroll && this.scroll.enable()
    },
    disable() {
      this.scroll && this.scroll.disable()
    },
    refresh() {
      this.scroll && this.scroll.refresh()
    },
    scrollTo() {
      this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
    },
    scrollToElement() {
      this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
    }
  },
  created() {},
  mounted() {
    setTimeout(() => {
      this._initScroll()
      // window.addEventListener('resize', () => {
      //   if (!this.scroll) {
      //     return
      //   }
      //   this.refresh()
      // })
    }, 20)
  }
}
</script>
<style lang="stylus" scoped>
.b-scroll{}
</style>